<?php
    define('EXPONENT', "ABSoft - Auto Mailling System");
    date_default_timezone_set("Asia/Ho_Chi_Minh");
    include("func.php");
    define('BASE',__realpath(dirname(__FILE__)).'/../');    
    include("../conf/config.php");
    // inital the database connection
    require_once("../subsystems/database.php");

    $db = exponent_database_connect(DB_USER,DB_PASS,DB_HOST.':'.DB_PORT,DB_NAME);
    //Thong ke
    $total=0;
    $not_found=0;
    $profiles=$db->selectObjects('profilemodule_profile');  
    //For case
    $cases=$db->selectObjects('profilemodule_cases');  
    $user_log='<head>
        <meta charset="UTF-8" /><head>';  
    foreach ($profiles as $k=>$v)
    {
        
        $user=$db->selectObject('user',"id='".$v->user_id."'");  
        //Not include admin
        if(($user->is_admin==1)||($user->is_acting_admin==1))
        continue;
        //Case
        $user_log.='<br><b>'.$v->firstname.' '.$v->lastname.'</b>('.$v->email.' )<br>';
        $cases=$db->selectObjects('profilemodule_cases', "user_id='".$v->user_id."'");
        $case_log='';
        foreach ($cases as $k=>$v)
        {   
                    
            if(!file_exists(BASE.$v->file_video))
                $case_log.=writelog_file('',$v->file_video);
                elseif($v->file_video!="") writelog_file(1);
            if(!file_exists(BASE.$v->file_pdf))
                $case_log.=writelog_file('',$v->file_pdf);
                elseif($v->file_pdf!="") writelog_file(1);
        }
        if($case_log!='')
            $user_log.='Cases:<br>'.$case_log;
        //
        $documents=$db->selectObjects('profilemodule_documents', "user_id='".$v->user_id."'");
        $doc_log='';
        foreach ($documents as $k=>$v)
        {            
            if(!file_exists(BASE.$v->xls_file))
                $doc_log.=writelog_file('',$v->xls_file);
                elseif($v->xls_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->flv_file))
                $doc_log.=writelog_file('',$v->flv_file);
                elseif($v->flv_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->mp3_file))
                $doc_log.=writelog_file('',$v->mp3_file);
                elseif($v->mp3_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->doc_file))
                $doc_log.=writelog_file('',$v->doc_file);
                elseif($v->doc_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->pdf_file))
                $doc_log.=writelog_file('',$v->pdf_file);
                elseif($v->pdf_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->ppt_file))
                $doc_log.=writelog_file('',$v->ppt_file);
                elseif($v->ppt_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->img_file))
                $doc_log.=writelog_file('',$v->img_file);
                elseif($v->img_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->mp4_file))
                $doc_log.=writelog_file('',$v->mp4_file);
                elseif($v->mp4_file!="") writelog_file(1);
        }
        if($doc_log!='')
            $user_log.='Documents:<br>'.$doc_log;

        $invoices=$db->selectObjects('profilemodule_invoices', "user_id='".$v->user_id."'");
        $invoice_log='';
        foreach ($invoices as $k=>$v)
        {            
            if(!file_exists(BASE.$v->pdf_file))
                $invoice_log.=writelog_file('',$v->pdf_file);
                elseif($v->pdf_file!="") writelog_file(1);
            if(!file_exists(BASE.$v->img_file))
                $invoice_log.=writelog_file('',$v->img_file);    
                elseif($v->img_file!="") writelog_file(1);     
        }
        if($invoice_log!='')
            $user_log.='Invoices:<br>'.$invoice_log;
        //$cases=$db->selectObjects('profilemodule_cases', "user_id='".$v->user_id."'");


    }
    //For file
    $user_log.='<br><br>Files:<br>';
    $files=$db->selectObjects('profilemodule_invoices', "user_id='".$v->user_id."'");
    $$files_log='';
    foreach ($files as $k=>$v)
        {            
            if(!file_exists(BASE.'/'.$v->directory.''.$v->filename))
                $$files_log.=writelog_file('','/'.$v->directory.''.$v->filename);            
        }
        
            $user_log.=$files_log;

    echo 'Total:'.$total.'<br>Not found:'.$not_found.'<br>'.$user_log;

    /*Search have file but not have data
        $all_dir = glob(BASE.'/files/profiles/*.*', GLOB_BRACE);
        foreach ($all_dir as $k=>$v)
        {
            
        }
    */

?>
